package com.educate.book.web.controller;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.educate.book.model.bo.BookCatalogueBO;
import com.educate.book.model.bo.ItemBO;
import com.educate.book.rpc.api.ItemService;
import com.educate.common.web.R;

import lombok.extern.log4j.Log4j2;

@Log4j2
@RestController
@RequestMapping("item")
public class ItemController {

    @Reference(version = "${api.service.version}",
            application = "${dubbo.application.id}",
            url = "${dubbo.application.url}",timeout=2000)
    private ItemService itemService;

    /**
     *  请求参数：{"desc":"0-3岁","name":"0-3岁","parentId","XXXXXXXX"}
     *  desc:描述
     *  name:名称
     *  parentId：父ID
     *  
 		返回结果:{"code":"000000","msg":"success","data":"{\"id\":\"a19a467237b2470e98a5ccb4286c3ca1\"}"}
 		id:为新增成功的ID
 		
 		返回结果：{"code":"000001","msg":"name duplicate"}
 		名称重复
     * @param bo
     * @return
     */
    @PostMapping(value = "/create")
    public Object create(@RequestBody ItemBO bo) {
    	String Id = itemService.create(bo);
    	JSONObject json = new JSONObject();
    	if("-1".equals(Id)) {
    		json.put("code", "000001");
    		json.put("msg", "name duplicate");
    		return json.toJSONString();
    	}
    	json.put("id", Id);
        return R.ok(json.toJSONString());
    }

    /**
     * 请求参数：{"itemId":"aff8389ff1534522884780c3ab1f02b7"}
 	        返回结果:{"code":"000000","msg":"success"}
     * @param bo
     * @return
     */
    @PostMapping(value = "/delete")
    public R delete(@RequestBody ItemBO bo) {
    	itemService.delete(bo);
        return R.ok();
    }
    /**
     * 根据ID删除分类
     * url:http://xxxxxx:port/delete/{itemId}
     * @param itemId
     * @return
     */
    @PostMapping(value = "/delete/{itemId}")
    public R delete(@PathVariable String itemId) {
    	itemService.delete(itemId);
        return R.ok();
    }

    @PostMapping(value = "/detail/{itemId}")
    public R<ItemBO> detail(@PathVariable String itemId) {
    	ItemBO bo = itemService.detail(itemId);
        return R.ok(bo);
    }

    @PostMapping(value = "/detail")
    public R<ItemBO> detail(@RequestBody ItemBO bo) {
    	ItemBO detail = itemService.detail(bo);
        return R.ok(detail);
    }

    @PostMapping(value = "/update")
    public R<ItemBO> update(@RequestBody ItemBO bo) {
    	itemService.update(bo);
        return R.ok();
    }

    /**
     * 请求参数：{"keyword":"name","level":0,"name":"动物","page":1,"size":10,"sortAsc":true,"sortProps":["crateTime"]}
       返回结果:{"code":"000000","msg":"success","data":{"content":[{"itemId":"7d4c18c7257d499cb77d29b726cc3c25","parentId":"6f51f69ef6cc4aae96c467d3b15f10ef","name":"动物","desc":"动物","crateTime":"2018-07-02 10:22:17","level":2},{"itemId":"ssfd","parentId":"6f51f69ef6cc4aae96c467d3b15f10ef","name":"动物2","desc":"动物2","crateTime":"2018-07-02 10:22:17","level":2},{"itemId":"ssfd1","parentId":"ssfd","name":"动物3.2","desc":"动物3.1","crateTime":"2018-07-02 10:22:17","level":3},{"itemId":"ssfdas","parentId":"ssfd","name":"动物3.3","desc":"动物33","crateTime":"2018-07-02 10:22:17","level":3},{"itemId":"aaaa","parentId":"7d4c18c7257d499cb77d29b726cc3c25","name":"动物2-->3","desc":"动物2-->3","crateTime":"2018-07-02 10:22:17","level":3}],"size":10,"number":0,"numberOfElements":5,"first":true,"last":true,"totalPages":1,"totalElements":5}}

     * @param bo
     * @return
     */
    @PostMapping(value = "/query")
    public R<Page<ItemBO>> query(@RequestBody ItemBO bo) {
        Page<ItemBO> result = itemService.query(bo);
        return R.ok(result);
    }

    /**
     * 请求参数为空：
     * 
     * 返回值：
     * {
	"code": "000000",
	"msg": "success",
	"data": [{
		"itemId": "6f51f69ef6cc4aae96c467d3b15f10ef",
		"name": "0-3岁",
		"desc": "0-3岁",
		"crateTime": "2018-07-02 10:22:17",
		"childItem": [{
			"itemId": "7d4c18c7257d499cb77d29b726cc3c25",
			"parentId": "6f51f69ef6cc4aae96c467d3b15f10ef",
			"name": "动物",
			"desc": "动物",
			"crateTime": "2018-07-02 10:22:17",
			"childItem": [{
				"itemId": "aaaa",
				"parentId": "7d4c18c7257d499cb77d29b726cc3c25",
				"name": "动物2-->3",
				"desc": "动物2-->3",
				"crateTime": "2018-07-02 10:22:17",
				"level": 3
			}],
			"level": 2
		}, {
			"itemId": "ssfd",
			"parentId": "6f51f69ef6cc4aae96c467d3b15f10ef",
			"name": "动物2",
			"desc": "动物2",
			"crateTime": "2018-07-02 10:22:17",
			"childItem": [{
				"itemId": "ssfd1",
				"parentId": "ssfd",
				"name": "动物3.2",
				"desc": "动物3.1",
				"crateTime": "2018-07-02 10:22:17",
				"level": 3
			}, {
				"itemId": "ssfdas",
				"parentId": "ssfd",
				"name": "动物3.3",
				"desc": "动物33",
				"crateTime": "2018-07-02 10:22:17",
				"level": 3
			}],
			"level": 2
		}],
		"level": 1
	}, {
		"itemId": "03f37512b69f46968b561f8d2374bcca",
		"name": "生活",
		"desc": "生活",
		"crateTime": "2018-07-02 10:26:02",
		"level": 1
	}]
}
     * @return
     */
    @PostMapping(value = "/all")
    public Object list() {
        return R.ok(itemService.queryAll());
    }
    /**
     * 返回结果:{"code":"000000","msg":"success","data":{"itemId":"ssfd","parentId":"6f51f69ef6cc4aae96c467d3b15f10ef","name":"动物2","desc":"动物2","crateTime":"2018-07-02 10:22:17","level":2,"status":0,"showOrder":100}}
     * 查询某个分类下的所有分类
     * @param itemId
     * @return
     */
    @PostMapping(value = "/get/{itemId}")
    public Object get(@PathVariable String itemId) {
        return R.ok(itemService.getItemByParentId(itemId));
    } 

}
